package com.zxy.salenet.web.service.imple;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxy.salenet.web.mapper.SalenetCommMapper;
import com.zxy.salenet.web.mapper.SalenetIndentMapper;
import com.zxy.salenet.web.mapper.SalenetUserMapper;
import com.zxy.salenet.web.pojo.SalenetComm;
import com.zxy.salenet.web.pojo.SalenetIndent;
import com.zxy.salenet.web.pojo.SalenetUser;
import com.zxy.salenet.web.pojo.dto.UpdateCommDTO;
import com.zxy.salenet.web.pojo.vo.UserMessageVO;
import com.zxy.salenet.web.service.UserConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.*;

/**
 * @author: zxy
 * @version: 1.0
 * @description:    用户个人设置接口实现
 */
  /**
 * UserConfigServiceImpl
 */
@Service
public class UserConfigServiceImpl implements UserConfigService {

    @Autowired
    private SalenetUserMapper userMapper;
    @Autowired
    private SalenetCommMapper commMapper;
    @Autowired
    private SalenetIndentMapper indexMapper;
    /**
     * 加密盐
     */
    private String salt= "zxy";
    /**
     *  获取当前用户需要展示的信息
     * @param userId   用户uid
     * @param userName 当前用户名
     * @return
     */
    @Override
    public UserMessageVO selectUserMessageAll(Integer userId, String userName) {
        UserMessageVO userMessageVO = new UserMessageVO();
        // 根据当前userId查出对应的用户信息
        // 需要展示的信息
        // 用户真实姓名-不可改 用户名-不可改 用户性别-不可改 用户地址-可改 用户生日-不可改 用户邮箱-可改 用户手机号-可改 当前当前使用收货地址-可改
        SalenetUser currSalenet = userMapper.selectUserMessageByUserIdAndUserName(userId,userName);
        System.out.println(currSalenet);
        System.out.println(currSalenet);
        //根据当前使用收货地址id获取当前收货地址信息
        SalenetComm currComm = commMapper.selectCurrComm(currSalenet.getUserComm());
        //根据userId查出对应地址信息表
        List<SalenetComm> commList = commMapper.selectAllByUserId(userId);
        userMessageVO.setSalenetUser(currSalenet);
        userMessageVO.setCurrComm(currComm);
        userMessageVO.setCommList(commList);
        return userMessageVO;
    }

    /**
     * 修改用户密码
     * @param userId
     * @param userPayPass
     * @param newUerPassword
     * @return
     */
    @Override
    public Integer updateUserPass(Integer userId, String userPayPass, String newUerPassword) {
        //先验证输入的用户支付密码是否正确 如果错误返回 -1 正确进行修改
        //加密比较
        System.out.println(userPayPass);
        userPayPass = DigestUtils.md5DigestAsHex((salt+userPayPass).getBytes());
        Integer userPassTrue = userMapper.selectPayPassTrue(userPayPass,userId);
        System.out.println(userPassTrue);
        if(userPassTrue == 0){
            System.out.println("错误");
            //如果支付密码错误返回-1
            return -1;
        }
        //新密码进行加密
        System.out.println(newUerPassword);
        newUerPassword = DigestUtils.md5DigestAsHex((salt+newUerPassword).getBytes());
        System.out.println("新密码"+newUerPassword);
        Integer updateResult = userMapper.updatePayPassByUserId(newUerPassword,userId);
        System.out.println(updateResult);
        return updateResult;
    }

    /**
     * 添加常用地址
     * @param salenetComm
     * @return
     */
    @Override
    public Integer addCurrAddComm(SalenetComm salenetComm) {
        System.out.println(salenetComm);
        Integer insertResult = commMapper.insertCommById(salenetComm);
        System.out.println(insertResult);
        if(insertResult != 0){
            return insertResult;
        }
        return null;
    }

    /**
     * 删除常用地址
     * @param sid
     * @return
     */
    @Override
    public Integer deleteCurrComm(Integer sid) {
        Integer deleteCommResult = commMapper.deleteCommById(sid);
        return deleteCommResult;
    }

    /**
     * 修改个人地址
     * @param comm
     * @return
     */
    @Override
    public Integer updateComm(SalenetComm comm) {
        Integer updateResult = commMapper.updateByPrimaryKeySelective(comm);
        return updateResult;
    }

    /**
     * 获取所有的商品购买信息
     * @param userId
     * @return
     */
    @Override
    public Map<String,List<SalenetIndent>> queryAllBuyLog(Integer userId) {
        List<Integer> indentIdList = indexMapper.selectIndentId(userId);
        Map<String,List<SalenetIndent>> buyLog = new LinkedHashMap<>();
        for(Integer indentEnty : indentIdList){
            List<SalenetIndent> currIndent = indexMapper.selectBuyIndentId(indentEnty);
            buyLog.put(": "+indentEnty.toString(),currIndent);
        }
        return buyLog;
    }

    /**
     * 根据购买订单记录删除商品购买信息
     * @param indentId
     * @param userId
     * @return
     */
    @Override
    public Integer deleteBuyLog(Integer indentId, Integer userId) {

        Integer result= indexMapper.deleteByIndentId(indentId);

        return result;
    }

    /**
     * 更新用户信息
     * @param salenetUser
     * @return
     */
    @Override
    public Integer updateUserMessage(SalenetUser salenetUser) {
        int i = userMapper.updateByPrimaryKeySelective(salenetUser);
        return i;
    }

    /**
     * 修改用户主要地址
     * @param commDTO
     * @return
     */
    @Override
    public boolean updatePublicComm(UpdateCommDTO commDTO) {
        return userMapper.updateCommByUserId(commDTO.getUserId(),commDTO.getCommId()) == 1;
    }
}
